/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/
 
/**
 * Pages data container. 
 */
qx.Class.define("bip.AbstractPages",
{
  extend : qx.core.Object,
  
  type: "abstract",

  construct : function(pages)
  {
    this.base(arguments);

    this.__pages = pages;
  },


  members :
  {
    __pages : null,
    __currentName : null,

    /**
     * Returns the pages stored with the given name.
     * @param name {String} the name of the page.
     * @return {String|Undefined} Returns the page model
     */
    get : function(name) {
      var sample = this.__pages[name];
      if (sample) {
        this.__currentName = name;
      }
      return sample;
    },


    /**
     * Returns an array of all stored pages names.
     * @return {Array} The names of all pages.
     */
    getNames : function() {
      return qx.lang.Object.getKeys(this.__pages);
    },


    /**
     * Returns the last fetched page using the {@link #get} method.
     * @return {String} The code of the last loaded page.
     */
    getCurrent : function() {
      return this.__pages[this.__currentName];
    },


    /**
     * Returns the name of the last fetched page using the {@link #get} method.
     * @return {String} The name of the last loaded page of an empty string, if
     *   no page has been loaded to this point.
     */
    getCurrentName : function() {
      return this.__currentName || "";
    },


    /**
     * Check if a page with the given name is available.
     * @param name {String} The name of the page.
     * @return {Boolean} true, if the page is available.
     */
    isAvailable : function(name) {
      return this.__pages[name] != undefined;
    },
    
    /**
     * Calls a iterator function for each page
     *
     * @param iterator {Function} the function to call for each page
     *          first parameter is the pagne name, second is the page
     * @param object {var ? null} the object to use when calling the handler.
     *          (this object will be available via "this" in the iterator)
     * @return {void}
     */
    iteratePages : function(iterator, object) {
			for (var name in this.__pages) {
				var page = this.__pages[name];
				iterator.call(object, name, page);
			}
    }
  },



  /*
   *****************************************************************************
      DESTRUCTOR
   *****************************************************************************
   */

  destruct : function() {

    this.__currentName = null;
    for (var name in this.__pages) {
			var page = this.__pages[name];
			if (page != null) {page.dispose();}
		}
    this.__pages = null;
  }
});
